const path = require('path');
const fs = require('fs')
const inquirer = require('inquirer');
const config = require('../config.json')

async function main(cmd){
     if(cmd.list){
        console.log(config)
     }
     if(cmd.update){
         
        let answers={
            key: cmd.key,
            value: cmd.value,
        };
        if(!answers.key || !answers.value){
            let keys = Object.keys(config);
            answers = await inquirer.prompt([
                {
                    type:"list",
                    message:"请选择要修改的内容",
                    name:'key',
                    choices: keys,
                    default: keys[0],
                },
                {
                    type:"input",
                    message:"请输入新值",
                    name:'value',
                },
            ])
        }
        
        config[answers.key]=answers.value;
        fs.writeFile(path.resolve(__dirname, '../config.json'), JSON.stringify(config), 'utf-8', (err) => {
            if (err) console.log(err)
            // console.log(config)
            // process.exit()
        })
         
     }
}

module.exports = main;